﻿/*
   Copyright 2014 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using Flower.Directory.Scripting.Functions.Processes;
using Flower.Services;
using Flower.Services.Data;
using Jurassic;
using Jurassic.Library;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Flower.Directory.Scripting.Functions.Waiting
{
    internal class Wait : ClrFunction
    {
        public Wait(ScriptEngine engine)
            : base(engine.Function.InstancePrototype, "__wait", engine.Function.InstancePrototype)
        {
        }

        [JSCallFunction]
        public string Exec(string resource, object pidObj, string waitersFolder)
        {
            try
            {
                return Exec(Engine.GetDirectory(), resource, JsConvert.ObjectToString(pidObj), waitersFolder);
            }
            catch (JavaScriptException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new JavaScriptException(Engine, "Error", ex.Message, ex);
            }
        }

        public static string Exec(IDirectory dir, string resource, string pid, string waitersFolder)
        {
            if (pid != null)
            {
                string resourceWaitersFolder = resource + waitersFolder;
                var processLink = dir.GetInfo(String.Concat(resourceWaitersFolder, "/", pid));

                string waitLinkId;

                if (processLink != null)
                {
                    waitLinkId = processLink.Id;
                }
                else
                {
                    var processLinkEnt = new Link
                    {
                        Name = pid,
                        Target = pid,
                        AccessControlList = new List<AccessControlEntry>
                        {
                            new AccessControlEntry
                            {
                                Role = "Processor",
                                Permission = Permission.Read | Permission.Write
                            }
                        }
                    };

                    waitLinkId = dir.Save(resourceWaitersFolder, processLinkEnt);
                }

                ChangeProcessStatus.Exec(dir, pid, "Waiting");

                return waitLinkId;
            }
            else
            {
                return null;
            }
        }
    }
}
